home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / setup / vbnet / 21 adonet disconnected / adonetdisconnected / pagingform.vb < prev    next >
Encoding:
Text File  |  2002-03-16  |  11.4 KB  |  272 lines

  1. Imports System.Data
  2. Imports System.Data.OleDb
  3. Imports System.Data.SqlClient
  4.  
  5. Public Class PagingForm
  6.     Inherits System.Windows.Forms.Form
  7.  
  8. #Region " Windows Form Designer generated code "
  9.  
  10.     Public Sub New()
  11.         MyBase.New()
  12.  
  13.         'This call is required by the Windows Form Designer.
  14.         InitializeComponent()
  15.  
  16.         'Add any initialization after the InitializeComponent() call
  17.  
  18.     End Sub
  19.  
  20.     'Form overrides dispose to clean up the component list.
  21.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  22.         If disposing Then
  23.             If Not (components Is Nothing) Then
  24.                 components.Dispose()
  25.             End If
  26.         End If
  27.         MyBase.Dispose(disposing)
  28.     End Sub
  29.  
  30.     'Required by the Windows Form Designer
  31.     Private components As System.ComponentModel.IContainer
  32.  
  33.     'NOTE: The following procedure is required by the Windows Form Designer
  34.     'It can be modified using the Windows Form Designer.  
  35.     'Do not modify it using the code editor.
  36.     Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
  37.     Friend WithEvents btnFirst As System.Windows.Forms.Button
  38.     Friend WithEvents btnPrevious As System.Windows.Forms.Button
  39.     Friend WithEvents btnNext As System.Windows.Forms.Button
  40.     Friend WithEvents btnLast As System.Windows.Forms.Button
  41.     Friend WithEvents btnGoto As System.Windows.Forms.Button
  42.     Friend WithEvents txtPageNum As System.Windows.Forms.TextBox
  43.     Friend WithEvents lblRecords As System.Windows.Forms.Label
  44.     Friend WithEvents lblCurrPage As System.Windows.Forms.Label
  45.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  46.         Me.DataGrid1 = New System.Windows.Forms.DataGrid()
  47.         Me.btnFirst = New System.Windows.Forms.Button()
  48.         Me.btnPrevious = New System.Windows.Forms.Button()
  49.         Me.btnNext = New System.Windows.Forms.Button()
  50.         Me.btnLast = New System.Windows.Forms.Button()
  51.         Me.btnGoto = New System.Windows.Forms.Button()
  52.         Me.txtPageNum = New System.Windows.Forms.TextBox()
  53.         Me.lblRecords = New System.Windows.Forms.Label()
  54.         Me.lblCurrPage = New System.Windows.Forms.Label()
  55.         CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
  56.         Me.SuspendLayout()
  57.         '
  58.         'DataGrid1
  59.         '
  60.         Me.DataGrid1.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
  61.                     Or System.Windows.Forms.AnchorStyles.Left) _
  62.                     Or System.Windows.Forms.AnchorStyles.Right)
  63.         Me.DataGrid1.DataMember = ""
  64.         Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
  65.         Me.DataGrid1.Location = New System.Drawing.Point(16, 8)
  66.         Me.DataGrid1.Name = "DataGrid1"
  67.         Me.DataGrid1.Size = New System.Drawing.Size(560, 248)
  68.         Me.DataGrid1.TabIndex = 0
  69.         '
  70.         'btnFirst
  71.         '
  72.         Me.btnFirst.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left)
  73.         Me.btnFirst.Location = New System.Drawing.Point(16, 264)
  74.         Me.btnFirst.Name = "btnFirst"
  75.         Me.btnFirst.Size = New System.Drawing.Size(48, 24)
  76.         Me.btnFirst.TabIndex = 1
  77.         Me.btnFirst.Text = "<<"
  78.         '
  79.         'btnPrevious
  80.         '
  81.         Me.btnPrevious.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left)
  82.         Me.btnPrevious.Location = New System.Drawing.Point(72, 264)
  83.         Me.btnPrevious.Name = "btnPrevious"
  84.         Me.btnPrevious.Size = New System.Drawing.Size(48, 24)
  85.         Me.btnPrevious.TabIndex = 2
  86.         Me.btnPrevious.Text = "<"
  87.         '
  88.         'btnNext
  89.         '
  90.         Me.btnNext.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left)
  91.         Me.btnNext.Location = New System.Drawing.Point(200, 264)
  92.         Me.btnNext.Name = "btnNext"
  93.         Me.btnNext.Size = New System.Drawing.Size(48, 24)
  94.         Me.btnNext.TabIndex = 3
  95.         Me.btnNext.Text = ">"
  96.         '
  97.         'btnLast
  98.         '
  99.         Me.btnLast.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left)
  100.         Me.btnLast.Location = New System.Drawing.Point(256, 264)
  101.         Me.btnLast.Name = "btnLast"
  102.         Me.btnLast.Size = New System.Drawing.Size(48, 24)
  103.         Me.btnLast.TabIndex = 4
  104.         Me.btnLast.Text = ">>"
  105.         '
  106.         'btnGoto
  107.         '
  108.         Me.btnGoto.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left)
  109.         Me.btnGoto.Location = New System.Drawing.Point(360, 264)
  110.         Me.btnGoto.Name = "btnGoto"
  111.         Me.btnGoto.Size = New System.Drawing.Size(64, 24)
  112.         Me.btnGoto.TabIndex = 5
  113.         Me.btnGoto.Text = "Goto #"
  114.         '
  115.         'txtPageNum
  116.         '
  117.         Me.txtPageNum.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left)
  118.         Me.txtPageNum.Location = New System.Drawing.Point(424, 264)
  119.         Me.txtPageNum.Name = "txtPageNum"
  120.         Me.txtPageNum.Size = New System.Drawing.Size(48, 24)
  121.         Me.txtPageNum.TabIndex = 6
  122.         Me.txtPageNum.Text = ""
  123.         '
  124.         'lblRecords
  125.         '
  126.         Me.lblRecords.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left)
  127.         Me.lblRecords.Location = New System.Drawing.Point(480, 264)
  128.         Me.lblRecords.Name = "lblRecords"
  129.         Me.lblRecords.Size = New System.Drawing.Size(56, 16)
  130.         Me.lblRecords.TabIndex = 7
  131.         Me.lblRecords.Text = "Label1"
  132.         '
  133.         'lblCurrPage
  134.         '
  135.         Me.lblCurrPage.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left)
  136.         Me.lblCurrPage.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
  137.         Me.lblCurrPage.Location = New System.Drawing.Point(136, 264)
  138.         Me.lblCurrPage.Name = "lblCurrPage"
  139.         Me.lblCurrPage.Size = New System.Drawing.Size(56, 24)
  140.         Me.lblCurrPage.TabIndex = 8
  141.         Me.lblCurrPage.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
  142.         '
  143.         'PagingForm
  144.         '
  145.         Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17)
  146.         Me.ClientSize = New System.Drawing.Size(584, 293)
  147.         Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblCurrPage, Me.lblRecords, Me.txtPageNum, Me.btnGoto, Me.btnLast, Me.btnNext, Me.btnPrevious, Me.btnFirst, Me.DataGrid1})
  148.         Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  149.         Me.Name = "PagingForm"
  150.         Me.Text = "PagingForm"
  151.         CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
  152.         Me.ResumeLayout(False)
  153.  
  154.     End Sub
  155.  
  156. #End Region
  157.  
  158.     ' This code can run only on Biblio.mdb
  159.     Dim cn As New OleDbConnection(BiblioConnString)
  160.     Dim cmd As New OleDbCommand(sql, cn)
  161.     Dim da As OleDbDataAdapter
  162.  
  163.     ' You can change this as you prefer
  164.     Dim pageSize As Integer = 10
  165.     ' This is the number of records.
  166.     Dim recCount As Integer
  167.     ' This is the number of pages.
  168.     Dim pageCount As Integer
  169.     ' This is the current page number.
  170.     Dim currPage As Integer
  171.  
  172.     Dim ds As New DataSet()
  173.     Dim dt As DataTable = ds.Tables.Add("Titles")
  174.     Dim sql As String
  175.  
  176.     Private Sub PagingForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  177.         ' Evaluate number of pages.
  178.         GetPageNumber()
  179.         ' Bind the Titles table.
  180.         DataGrid1.DataSource = dt
  181.         ' Show the first page of results
  182.         btnFirst.PerformClick()
  183.     End Sub
  184.  
  185.     ' Evaluate number of pages in the result.
  186.     Sub GetPageNumber()
  187.         Dim closeOnExit As Boolean
  188.  
  189.         ' Open the connection if necessary.
  190.         If cn.State = ConnectionState.Closed Then
  191.             cn.Open()
  192.             closeOnExit = True
  193.         End If
  194.  
  195.         ' Evaluate number of records.
  196.         cmd.CommandText = "SELECT COUNT(*) FROM Titles"
  197.         recCount = CInt(cmd.ExecuteScalar())
  198.         ' Close the connection if it was closed.
  199.         If closeOnExit Then cn.Close()
  200.  
  201.         ' Evaluate number of pages.
  202.         pageCount = (recCount + pageSize - 1) \ pageSize
  203.         lblRecords.Text = " of " & pageCount.ToString
  204.     End Sub
  205.  
  206.     ' Display the N-th page of result.
  207.     Sub DisplayPage(ByVal n As Integer, ByVal sql As String)
  208.         ' Perform the query and display results.
  209.         cn.Open()
  210.         Dim da As New OleDbDataAdapter(sql, cn)
  211.         dt.Clear()
  212.         da.Fill(dt)
  213.         ' Uncomment next statement to update page count each time
  214.         ' the user displays a new page.
  215.         ' GetPageNumber
  216.         cn.Close()
  217.  
  218.         ' Remember current page number and display it.
  219.         currPage = n
  220.         lblCurrPage.Text = n.ToString
  221.  
  222.         ' Enable/disable buttons.
  223.         btnFirst.Enabled = (n > 1)
  224.         btnPrevious.Enabled = (n > 1)
  225.         btnNext.Enabled = (n < pageCount)
  226.         btnLast.Enabled = (n < pageCount)
  227.     End Sub
  228.  
  229.     ' Manage the four navigational buttons.
  230.  
  231.     Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
  232.         sql = String.Format("SELECT TOP {0} * FROM Titles ORDER BY ISBN", pageSize)
  233.         DisplayPage(1, sql)
  234.     End Sub
  235.  
  236.     Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
  237.         sql = String.Format("SELECT * FROM Titles WHERE ISBN IN (SELECT TOP {0} ISBN FROM Titles WHERE ISBN < '{1}' ORDER BY ISBN DESC) ORDER BY ISBN", pageSize, dt.Rows(0)("ISBN"))
  238.         DisplayPage(currPage - 1, sql)
  239.     End Sub
  240.  
  241.     Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
  242.         sql = String.Format("SELECT TOP {0} * FROM Titles WHERE ISBN > '{1}' ORDER BY ISBN", pageSize, dt.Rows(dt.Rows.Count - 1)("ISBN"))
  243.         DisplayPage(currPage + 1, sql)
  244.     End Sub
  245.  
  246.     Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
  247.         ' Evaluate number or records on last page.
  248.         Dim num As Integer = recCount - pageSize * (pageCount - 1)
  249.         sql = String.Format("SELECT * FROM Titles WHERE ISBN IN (SELECT TOP {0} ISBN FROM Titles ORDER BY ISBN DESC) ORDER BY ISBN", num)
  250.         DisplayPage(pageCount, sql)
  251.     End Sub
  252.  
  253.     ' Goto N-th page.
  254.     Private Sub btnGoto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGoto.Click
  255.         Try
  256.             Dim pageNum As Integer = CInt(txtPageNum.Text)
  257.             sql = String.Format("SELECT * FROM Titles WHERE ISBN IN (SELECT TOP {0} ISBN FROM Titles WHERE ISBN IN (SELECT TOP {1} ISBN FROM Titles ORDER BY ISBN) ORDER BY ISBN DESC) ORDER BY ISBN", pageSize, pageSize * pageNum)
  258.             DisplayPage(pageNum, sql)
  259.         Catch ex As Exception
  260.             Debug.WriteLine(ex.Message)
  261.             MessageBox.Show("Page # must be in the range [1," & pageCount.ToString & "]")
  262.         End Try
  263.     End Sub
  264.  
  265.     ' Enable the Goto button only if something has been typed inside the TextBox.
  266.     Private Sub txtPageNum_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPageNum.TextChanged
  267.         btnGoto.Enabled = txtPageNum.Text.Length > 0
  268.     End Sub
  269.  
  270. End Class
  271.  
  272.